package com.rla.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.rla.common.core.page.TableDataInfo;
import com.rla.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rla.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.rla.domain.bo.BaseHealthTargetBo;
import com.rla.domain.vo.BaseHealthTargetVo;
import com.rla.domain.BaseHealthTarget;
import com.rla.mapper.BaseHealthTargetMapper;
import com.rla.service.IBaseHealthTargetService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 健康数据指标Service业务层处理
 *
 * @author rla
 * @date 2025-07-30
 */
@RequiredArgsConstructor
@Service
public class BaseHealthTargetServiceImpl implements IBaseHealthTargetService {

    private final BaseHealthTargetMapper baseMapper;

    /**
     * 查询健康数据指标
     */
    @Override
    public BaseHealthTargetVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询健康数据指标列表
     */
    @Override
    public TableDataInfo<BaseHealthTargetVo> queryPageList(BaseHealthTargetBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseHealthTarget> lqw = buildQueryWrapper(bo);
        Page<BaseHealthTargetVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询健康数据指标列表
     */
    @Override
    public List<BaseHealthTargetVo> queryList(BaseHealthTargetBo bo) {
        LambdaQueryWrapper<BaseHealthTarget> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaseHealthTarget> buildQueryWrapper(BaseHealthTargetBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaseHealthTarget> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getIconUrl()), BaseHealthTarget::getIconUrl, bo.getIconUrl());
        lqw.like(StringUtils.isNotBlank(bo.getTargetName()), BaseHealthTarget::getTargetName, bo.getTargetName());
        lqw.eq(StringUtils.isNotBlank(bo.getTargetUnit()), BaseHealthTarget::getTargetUnit, bo.getTargetUnit());
        lqw.eq(StringUtils.isNotBlank(bo.getIsStopUse()), BaseHealthTarget::getIsStopUse, bo.getIsStopUse());
        lqw.orderByAsc(BaseHealthTarget::getSort);
        return lqw;
    }

    /**
     * 新增健康数据指标
     */
    @Override
    public Boolean insertByBo(BaseHealthTargetBo bo) {
        BaseHealthTarget add = BeanUtil.toBean(bo, BaseHealthTarget.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改健康数据指标
     */
    @Override
    public Boolean updateByBo(BaseHealthTargetBo bo) {
        BaseHealthTarget update = BeanUtil.toBean(bo, BaseHealthTarget.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaseHealthTarget entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除健康数据指标
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
